﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Experiements.WebTechs2013.Service
{
    public class ProductsService
    {
        private static readonly List<Product> _Products;

        static ProductsService()
        {
            _Products = CreateProducts().OrderBy(p => p.Nonsense).ToList();
        }

        private static IEnumerable<Product> CreateProducts()
        {
            var rand = new Random();

            const int max = 90;

            for (int cnt = 0; cnt < max; cnt++)
            {
                yield return new Product
                {
                    Id = Guid.NewGuid(),
                    Name = "Product #" + (cnt+1),
                    Date = DateTime.Now.AddDays(-rand.Next(max)),
                    Description = "Bla, bla, bla, " + cnt + ", bla",
                    Nonsense = rand.Next(max),
                    State = RecordStates.Normal,
                };
            }
        }

        public IEnumerable<Product> GetProducts()
        {
            return _Products;
        }

        public Product GetProduct(Guid id)
        {
            return _Products.Where(p => p.Id == id).FirstOrDefault();
        }

      
        public void SetProducts(IEnumerable<Product> products)
        {
            foreach (var prod in products)
            {
                if (prod.State.HasFlag(RecordStates.RequestNew))
                {
                    if (!prod.State.HasFlag(RecordStates.RequestDelete) || prod.State.HasFlag(RecordStates.RequestUndelete))
                    {
                        prod.Id = Guid.NewGuid();
                        prod.State = RecordStates.Normal;
                        _Products.Add(prod);
                    }
                    continue;
                }

                var srvProd = _Products.Where(p => p.Id == prod.Id).SingleOrDefault();
                if (srvProd == null)
                    continue;

                if (prod.State.HasFlag(RecordStates.RequestDelete))
                {
                    srvProd.State = RecordStates.Deleted;
                }

                if (prod.State.HasFlag(RecordStates.RequestUndelete))
                {
                    srvProd.State = RecordStates.Normal;
                }

                if (prod.State.HasFlag(RecordStates.RequestModify))
                {
                    srvProd.Name = prod.Name;
                    srvProd.Date = prod.Date;
                    srvProd.Description = prod.Description;
                }
            }
        }

        public void SetProduct(Product product)
        {
            SetProducts(new [] { product });
        }

        public void RemoveProduct(Guid id)
        {
            Product product = null;

            if (id != Guid.Empty)
                product = _Products.Where(p => p.Id == id).FirstOrDefault();

            product.State |= RecordStates.RequestDelete;

            SetProducts(new[] { product });
        }
    }
}
